package com.cndw.kungfu.model.pvp.api;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.cndw.kungfu.config.ConstantsConfig;
import com.cndw.kungfu.ext.LogUtil;
import com.cndw.kungfu.model.pvp.war.WarFighter;
import com.cndw.kungfu.model.pvp.war.WarPlayer;
import com.cndw.rpg.framework.ResponseMessage;

public class WarApiClient {

	@SuppressWarnings("unchecked")
	public static List<WarPlayer> getAllPlayers(int serverId) {

		Object message = ApiClient.setdApi(serverId, ApiConfig.warPlayers);
		if (message == null) {
			LogUtil.getMain().debug("err cross null " + serverId);
			return null;
		}
		Map<String, Object> dataMap = (Map<String, Object>) message;
		if (dataMap.containsKey("sid")) {
			int sid = (Integer) dataMap.get("sid");
			if (sid != serverId) {
				LogUtil.getMain().error("err cross call " + serverId + "->" + sid);
			}
		}
		if (dataMap.containsKey("warPlayers")) {
			Object warPlayers = dataMap.get("warPlayers");
			if (warPlayers.getClass().isArray()) {
				List<WarPlayer> allPlayers = new ArrayList<WarPlayer>();
				Object[] pArray = (Object[]) dataMap.get("warPlayers");
				for (Object warPlayer : pArray) {
					allPlayers.add((WarPlayer) warPlayer);
				}
				return allPlayers;

			} else {
				List<WarPlayer> pList = (List<WarPlayer>) dataMap.get("warPlayers");

				return pList;
			}

		}

		return null;

	}

	public static WarFighter getWarFighter(int serverId, long pid) {
		Object message = ApiClient.setdApi(serverId, ApiConfig.warFighter, pid);
		if (message == null) {
			LogUtil.getMain().debug("err cross null " + serverId);
			return null;
		}
		if (message instanceof WarFighter) {
			return (WarFighter) message;
		}
		return null;
	}

	private static Map<String, Object> getCenterData(String method, Object... parms) {
		Object mesage = ApiClient.setdApi(ApiCore.crossSid(), method, parms);

		if (mesage != null) {
			try {
				@SuppressWarnings("unchecked")
				Map<String, Object> data = (Map<String, Object>) mesage;
				return data;
			} catch (Exception e) {
				e.printStackTrace();
			}

		}
		return new HashMap<String, Object>();
	}


	public static Map<String, Object> getCrossShow() {
		return getCenterData(ApiConfig.warShow, ConstantsConfig.getSysCgServiceId());
	}

	public static Map<String, Object> getCrossVs(long uid) {
		return getCenterData(ApiConfig.warVs, ConstantsConfig.getSysCgServiceId(), uid);
	}

	public static Map<String, Object> getCrossPromotion(long uid) {
		return getCenterData(ApiConfig.warPromotion, ConstantsConfig.getSysCgServiceId(), uid);
	}

	public static Map<String, Object> getCrossPromotionvs(long sourceId, long targetId) {
		return getCenterData(ApiConfig.warPromotionvs, ConstantsConfig.getSysCgServiceId(), sourceId, targetId);
	}

	public static Map<String, Object> getCrossPromotionMy(long uid) {
		return getCenterData(ApiConfig.warPromotionMy, ConstantsConfig.getSysCgServiceId(), uid);
	}

	public static Map<String, Object> getCrossBet(long uid, int camType, long targetId, int asset) {
		return getCenterData(ApiConfig.warBet, ConstantsConfig.getSysCgServiceId(), uid, camType, targetId, asset);
	}

	public static Map<String, Object> getCrossBetList(long uid) {
		return getCenterData(ApiConfig.warBetList, ConstantsConfig.getSysCgServiceId(), uid);
	}

	public static Map<String, Object> getCrossOrderPower() {
		return getCenterData(ApiConfig.warOrderPower, ConstantsConfig.getSysCgServiceId());
	}

	public static void crossFlower(long playerId, String playerName, int type) {
		getCenterData(ApiConfig.warCrossFlower, ConstantsConfig.getSysCgServiceId(), playerId, playerName, type);
	}

	public static Object getWarFlowers() {
		return getCenterData(ApiConfig.warFlowerList, ConstantsConfig.getSysCgServiceId());
	}

	public static Object crossPlayer(int serverId, long uid, int type) {
		return ApiClient.setdApi(serverId, ApiConfig.crossInfo, uid, type);
	}

	/**
	 * 专门用于天下第一接口
	 * 
	 * @param method
	 * @param parms
	 * @return
	 */
	public static ResponseMessage worldApiResp(String method, Object... parms) {
		return ApiClient.apiMessage(ApiCore.crossSid(), method, 0, parms);
	}

	public static Map<String, Object> worldApiMap(String method, Object... parms) {
		ResponseMessage msg = worldApiResp(method, parms);
		if (msg == null || msg.m == null) {
			return null;
		}
		try {
			@SuppressWarnings("unchecked")
			Map<String, Object> data = (Map<String, Object>) msg.m;
			return data;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	
	public static Object sendApiByTimeOut(int serverId, String method, int readTimeOut, Object... parms) {
		return ApiClient.sendApiByTimeOut(serverId, method, readTimeOut, parms);
	}

}
